﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using T.ApiResults;
using T.Vehicleservice.Domain.Dtos;
using T.Vehicleservice.Domain.T.Vehicleservice.Model;
using T.Vehicleservice.Read.API.Application.Command.TireUsageRecordCommand;

namespace T.Vehicleservice.Read.API.Controllers
{
    /// <summary>
    /// 轮胎使用记录控制器(读)
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TireUsageRecordController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="mediator"></param>
        public TireUsageRecordController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 显示轮胎使用记录
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<TireUsageRecord>>> Show([FromQuery]TireUsageRecordShowCommand command)
        {
            ApiResult<PageDto<TireUsageRecord>> result=new ApiResult<PageDto<TireUsageRecord>>(){};
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                result.Data=await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 返填轮胎使用记录
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<TireUsageRecord>> Backfill([FromQuery] TireUsageRecordBackfillCommand command)
        {
            ApiResult<TireUsageRecord> result = new ApiResult<TireUsageRecord>() { };
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
